package com.boilerCloud.controller.repair;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.repair.RepairPlan;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.dto.work.WorkOrder;
import com.boilerCloud.service.log.ILogService;
import com.boilerCloud.service.repair.IRepairPlanService;


@RestController
@RequestMapping("/repair/")
public class RepairPlanControler {
	private static final Logger log = LoggerFactory.getLogger(RepairPlanControler.class);
	@Resource
	private IRepairPlanService service;


	@RequestMapping(value = "repairPlanList")
	public ModelAndView repairPlanList() {
		return new ModelAndView("/views/after/repairPlan.html");
	}
	
	

	/**
	 * 查询设备保修列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryRepairPlanList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryRepairPlanList(@ModelAttribute RepairPlan model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<RepairPlan> page = new HzlqswPage<RepairPlan>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询设备保修列表失败");
			log.error("查询设备保修列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 维修工单
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("创建维修工单")
	@RequestMapping(value = "saveWorkOrder", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult saveWorkOrder(
			@ModelAttribute  WorkOrder model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			User user = (User)request.getSession().getAttribute("usersession");
			model.setCreatorId(user.getId());
			model.setCreatorName(user.getName());
			service.saveWorkOrder(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("设备报修派单失败");
			log.error("设备报修派单",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "getRepairPlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getRepairPlan(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			RepairPlan model = service.getById(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取设备保修信息失败");
			log.error("获取设备保修信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 新增设备保修
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增设备保修")
	@RequestMapping(value = "addRepairPlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addRepairPlan(
			@ModelAttribute  RepairPlan model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增设备保修失败");
			log.error("新增设备保修异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除设备保修
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delRepairPlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delRepairPlan(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除设备保修失败");
			log.error("删除设备保修异常",e);
			return rst;
		}
	}
	


	

}
